import 'package:boss/color_constant.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/widget/button/fixed_bottom_btn.dart';
import 'package:boss/ui/widget/custom_divier.dart';
import 'package:boss/ui/widget/modal/modal.dart';
import 'package:flutter/material.dart';
import 'package:hi_common/hi_common.dart';

Future showAlert<T>(
  BuildContext context, {
  dynamic content,
  String title,
  ValueChanged<BuildContext> onConfirm,
  String confirmText = '确认',
  ValueChanged<BuildContext> onCancel,
  String cancelText = '取消',
  bool barrierDismissible = false,
  bool buttonOnly = false,
}) {
  return showModal<T>(
    context,
    builder: (context) => _Alert(
      title: title,
      content: content,
      confirmText: confirmText,
      cancelText: cancelText,
      onConfirm: onConfirm,
      onCancel: onCancel,
      buttonOnly: buttonOnly,
    ),
    barrierDismissible: barrierDismissible,
  );
}

class _Alert extends StatefulWidget {
  final String title;
  final dynamic content;
  final String confirmText;
  final ValueChanged<BuildContext> onConfirm;
  final ValueChanged<BuildContext> onCancel;
  final String cancelText;
  final bool buttonOnly;

  const _Alert({
    Key key,
    this.title,
    this.content,
    this.onConfirm,
    this.confirmText,
    this.onCancel,
    this.cancelText,
    this.buttonOnly,
  }) : super(key: key);

  @override
  __AlertState createState() => __AlertState();
}

class __AlertState extends State<_Alert> {
  @override
  Widget build(BuildContext context) {
    return Container(
      width: 600.w,
      child: Column(
        children: <Widget>[
          if (widget.title != null)
            Container(
              alignment: Alignment.center,
              padding: EdgeInsets.only(top: 20.w),
              child: Text(widget.title, style: TextStyleConstant().normal_1_32.copyWith(fontWeight: FontWeight.w500)),
            ),
          Container(
            alignment: Alignment.center,
            constraints: BoxConstraints(minHeight: 190.w),
            padding: EdgeInsets.symmetric(horizontal: 30.w, vertical: 20.w),
            child: _buildContent(),
          ),
          if (widget.buttonOnly != true) CustomDivider(margin: EdgeInsets.symmetric(horizontal: 20.w)),
          Container(
            height: widget.buttonOnly != true ? 110.w : null,
            child: widget.buttonOnly == true
                ? Row(
                    children: <Widget>[
                      Expanded(
                        flex: 1,
                        child: Container(),
                      ),
                      Expanded(
                        flex: 4,
                        child: Padding(
                          padding: EdgeInsets.only(
                            bottom: 12.w,
                          ),
                          child: FixedBottomBtn(
                            isShowShadow: false,
                            title: widget.confirmText,
                            onTap: () => widget.onConfirm(context),
                          ),
                        ),
                      ),
                      Expanded(
                        flex: 1,
                        child: Container(),
                      ),
                    ],
                  )
                : Row(
                    children: <Widget>[
                      if (widget.onCancel != null)
                        Expanded(
                          child: GestureDetector(
                            onTap: () => widget.onCancel(context),
                            behavior: HitTestBehavior.translucent,
                            child: Align(
                              alignment: Alignment.center,
                              child: Text(
                                widget.cancelText,
                                style: TextStyleConstant().origin_32,
                              ),
                            ),
                          ),
                        ),
                      if (widget.onCancel != null)
                        Container(
                          width: 1.px,
                          height: 90.w,
                          color: ColorConstant.e6e6,
                        ),
                      Expanded(
                        child: GestureDetector(
                          onTap: () => widget.onConfirm(context),
                          behavior: HitTestBehavior.translucent,
                          child: Align(
                            alignment: Alignment.center,
                            child: Text(
                              widget.confirmText,
                              style: TextStyleConstant().blue_32,
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
          ),
        ],
      ),
    );
  }

  Widget _buildContent() {
    if (widget.content is String) {
      return Text(
        widget.content,
        style: TextStyle(
          fontSize: 28.sp,
          color: ColorConstant.ff66,
          height: 1.75,
        ),
        textAlign: TextAlign.center,
      );
    }

    if (widget.content is Widget) {
      return widget.content;
    }

    return SizedBox();
  }
}
